/* 
Vuex 数据持久化  每次刷新页面可以把数据保存到本地
方式1 vuex-persistedstate 插件 
使用方式: store/index.js  使用插件 plugins: [createPersistedState()],
*/

export default{
  namespaced:true ,
  state: JSON.parse(localStorage.getItem('vuex_count'))||{
    count: 15,
  },


  getters: {
    doubleCount(state) {
      return state.count * 2;
    },
  },


  mutations: {
    increment(state) {
      state.count++;
      localStorage.setItem("vuex_count", JSON.stringify(state));
    },

    //每次加n
    incrementN(state, { n }) {
      state.count += n;
      localStorage.setItem("vuex_count", JSON.stringify(state));
    },
  },


  actions:{
    incrementWait({ commit }) {
      setTimeout(() => {
        commit("increment");
      }, 1000);
    },

    incrementNWait({ commit }, payload) {
      setTimeout(() => {
        commit("incrementN", payload);
      }, 1000);
    },
  }
}